import { AppModel } from '../../../../model/AppModel'

import { createGPIOMenu } from '../BlockPackage'

 
export default {

    category: Blockly.Categories.bepart ,
    
    color: [
        Blockly.Colours.bepart.primary,
        Blockly.Colours.bepart.secondary,
        Blockly.Colours.bepart.tertiary
    ] ,

    previousStatement: true ,
    nextStatement: true ,

    init(model: AppModel) {
        this.appendDummyInput()
            .appendField("在")
            .appendField(createGPIOMenu(model, true), 'stud')
            
        this.appendValueInput("freqency")
            .appendField("以频率(HZ)")
            
        this.appendValueInput("value")
            .appendField("输出PWM")
    } ,

    run (args: any, util: any, runtime: any, model:AppModel) {
        if(!model.masterPart) {
            return
        }
        // stud.pwm.setting
        let stud = model.masterPart.findStudFromName(args.stud) 
        if(!stud || !stud.pwm.setting)
            return
        stud.pwm.setting.frequency = parseInt(args.freqency)
        stud.pwm.value = parseInt(args.value)
    } ,

    coder: {
        JavaScript(block: any, coder: any) {
            
            let pin = block.getFieldValue('stud')
            let freqency = coder.valueToCode(block,'freqency')
            let value = coder.valueToCode(block,'value')

            return `${coder.indentString()}pin(${pin}).pwm(${value}, ${freqency}, 10)\n`
        }
    }
}
